<template>
<div>
  <div class="weui-cells__title">输入抢宿舍相关信息</div>
  <div class="weui-cells weui-cells_form">
    <div  class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">认证码</label>
      </div>
      <div class="weui-cell__bd">
        <input v-model="selfStudentNo" class="weui-input" type="text" maxlength="12" placeholder="输入你的学号">
      </div>
      <div class="weui-cell__bd">
        <input v-model="selfAuthCode" class="weui-input" type="text" maxlength="6" placeholder="输入你的6位认证码">
      </div>
    </div>

    <div  class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">同住人1</label>
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[0].studentNo" class="weui-input" type="text" maxlength="12" placeholder="输入同住人的学号">
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[0].AuthCode" class="weui-input" type="text" maxlength="6" placeholder="输入同住人的认证码">
      </div>
    </div>

    <div  class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">同住人2</label>
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[1].studentNo" class="weui-input" type="text" maxlength="12" placeholder="输入同住人的学号">
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[1].AuthCode" class="weui-input" type="text" maxlength="6" placeholder="输入同住人的认证码">
      </div>
    </div>

    <div  class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">同住人3</label>
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[2].studentNo" class="weui-input" type="text" maxlength="12" placeholder="输入同住人的学号">
      </div>
      <div class="weui-cell__bd">
        <input v-model="mateList[2].AuthCode" class="weui-input" type="text" maxlength="6" placeholder="输入同住人的认证码">
      </div>
    </div>

    <!--        选择宿舍楼 -->
    <div  class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">宿舍楼</label>
      </div>
      <div class="weui-cell__bd">
        <select class="weui-select"  v-model="buildingId">
          <option value="1">5号楼</option>
          <option value="2">13号楼</option>
        </select>
      </div>
    </div>
  </div>
  <div class="weui-btn-area">
    <button id="btn1"  class="weui-btn weui-btn_primary" @click="submit">抢宿舍</button>
  </div>

</div>
</template>


<script>
import axios from "axios";
// import QS from 'qs';

export default {
  name: "SelectDorm",
  data(){
    return{
      mateList:[
        {
          studentNo: '',
          AuthCode: '',
        },
        {
          studentNo: '',
          AuthCode: '',
        },
        {
          studentNo: '',
          AuthCode: '',
        }
      ],

      teammatesMap: '',
      selfStudentNo: '',
      selfAuthCode: '',
      buildingId: '',

      // ruleForm: {
      //   selfStudentNo: '',
      //   selfAuthCode: '',
      //   building: '',
      //   mates:[
      //
      //   ]
      // },
      // rules: {
      //   selfStudentNo: [
      //     { required: true, message: '请输入自己的学号', trigger: 'blur' },
      //     { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
      //   ],
      //   selfAuthCode: [
      //     { required: true, message: '请输入自己的认证码', trigger: 'blur' },
      //     { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
      //   ],
      //   building: [
      //     { required: true, message: '请选择宿舍楼', trigger: 'change' }
      //   ],
      // }
    }
  },
  methods:{
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },

    submit(){
      let token = localStorage.getItem("token")
      console.log(this.mateList)
      this.teammatesMap = new Map()

      for (let i = 0; i < this.mateList.length; i++) {
        if(this.mateList[i].studentNo !== '' && this.mateList[i].AuthCode !== ''){
          this.teammatesMap.set(this.mateList[i].studentNo,this.mateList[i].AuthCode)
        }
      }

      let param= {
        "buildingId": this.buildingId,
        "selfStudentNo": this.selfStudentNo,
        "selfAuthCode": this.selfAuthCode,
        // "teammatesMap": this.teammatesMap.toString(),
        "otherStuNo0": this.mateList[0].studentNo,
        "otherAuthCode0": this.mateList[0].AuthCode,
        "otherStuNo1": this.mateList[1].studentNo,
        "otherAuthCode1": this.mateList[1].AuthCode,
        "otherStuNo2": this.mateList[2].studentNo,
        "otherAuthCode2": this.mateList[2].AuthCode,
      }
      // let param = new URLSearchParams();
      // param.append("buildingId",this.buildingId)
      // param.append("selfStudentNo",this.selfStudentNo)
      // param.append("selfAuthCode",this.selfAuthCode)
      // param.append("teammatesMap",this.teammatesMap)

      console.log(param)

      axios.defaults.withCredentials = true
      document.cookie = 'access-token' + token
      // axios.defaults.headers.post['Content-Type'] = 'application/json';

      // axios({
      //   method: 'post',
      //   url: 'http://localhost:8090/order/create',
      //
      // })

      this.$axios.post("http://localhost:8090/order/create",param)
      .then(res => {
          if(res.data.code == 200){
            alert("抢宿舍成功!请在结果页查看详情")
            localStorage.setItem("orderId", res.data.result.orderId)
          }else{
            alert(res.data.message)
          }
      })
    },
  },

  computed:{

  },
}
</script>

<style scoped>
  #teammates{
    margin-left: 300px;
    text-align: center;
  }
</style>